/**
 * 客户资料维护 - 同步ERP
 * @author miaoyl
 * @date 2018/01/14
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        table,myTable,
        table_where = {},
        cols = [[
            {type: 'checkbox'},
            {field: 'MB001', width: '200', title: '公司编号'},
            {field: 'MB002', width: '400', title: 'ERP数据库'}
        ]],
        gwak001_list
    ;

    var Gwai300Erp = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai300_syncERP',
                /*_searchs: [{
                 id: 'gwai300_syncERP', tableId: 'gwai300_syncERP_table', datas: cols[0]
                 }],*/
                _loadTplsFull: [{
                    dom: 'gwai300_syncERP_result', tpl: 'gwai300_syncERP_result'
                }]
            }, win);
        },

        _call: function (obj) {
            gwak001_list = obj.gwak001_list;
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                myTable= table.render({
                    elem: app.getByid(doc, 'gwai300_syncERP_table'),
                    url: g_ctxPath + 'crm/gwai300/getERPList',
                    id: 'gwai300_syncERP_table',
                    height: 330,
                    width: 400,
                    cols: cols,
                    page:true,
                    where: table_where
                });

                var tipsIndex = null;

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        _ready: function () {
            var vue = new Vue({
                el: '#gwai300_syncERP',
                data: '',
                methods: {
                    cancel: function () {
                        layer.close(layer.index);
                    },
                    sure: function () {
                        var checkStatus = table.checkStatus('gwai300_syncERP_table');
                        if (checkStatus.data.length > 0) {
                            var _xdata = [];
                            checkStatus.data.forEach(function (v) {
                                _xdata.push(v.MB001);
                            });
                            app.ajax({
                                xurl: 'crm/gwai300/insertERP',
                                xdata: {
                                    mb001_list: _xdata.toString(),
                                    gwak001_list: gwak001_list.toString()
                                },
                                cb: function (result) {
                                    layer.close(layer.index);
                                    if (result.ok) {
                                        self._openTplFull({
                                            tag: 'gwai300_syncERP_result',
                                            tagData: {
                                                list: result.data.data
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: '同步结果',
                                                area: ['720px', '430px']
                                            }
                                        });
                                    }
                                }
                            });
                        } else {
                            app.showError('请先选择数据！');
                            return;
                        }
                    }
                },
                created: function () {
                    self._setEleIsReady('gwai300_syncERP');
                }
            });

            self.initTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwai300Erp();

});